package com.agritech.smartfarming.model;

import lombok.Data;
import com.agritech.smartfarming.observer.EnvironmentObserver;
import com.agritech.smartfarming.strategy.PathPlanningStrategy;

/**
 * 农机设备基类，实现了观察者接口
 */
@Data
public abstract class AgriculturalMachine implements EnvironmentObserver {
    protected String id;                      // 设备ID
    protected String name;                    // 设备名称
    protected String type;                    // 设备类型
    protected Position position;              // 当前位置
    protected double fuelLevel;               // 燃油水平 (%)
    protected double batteryLevel;            // 电池电量 (%)
    protected MachineStatus status;           // 设备状态
    protected PathPlanningStrategy pathStrategy; // 路径规划策略
    
    public AgriculturalMachine(String id, String name, String type) {
        this.id = id;
        this.name = name;
        this.type = type;
        this.position = new Position(0, 0, 0);
        this.fuelLevel = 100.0;
        this.batteryLevel = 100.0;
        this.status = MachineStatus.IDLE;
    }
    
    /**
     * 启动设备
     */
    public void start() {
        if (status == MachineStatus.IDLE || status == MachineStatus.MAINTENANCE) {
            status = MachineStatus.WORKING;
            System.out.println(name + " (" + type + ") 已启动，开始工作。");
        } else {
            System.out.println(name + " 当前状态为 " + status + "，无法启动。");
        }
    }
    
    /**
     * 停止设备
     */
    public void stop() {
        if (status == MachineStatus.WORKING || status == MachineStatus.MOVING) {
            status = MachineStatus.IDLE;
            System.out.println(name + " (" + type + ") 已停止工作。");
        } else {
            System.out.println(name + " 当前状态为 " + status + "，无需停止。");
        }
    }
    
    /**
     * 移动到指定位置
     */
    public void moveTo(Position targetPosition) {
        status = MachineStatus.MOVING;
        System.out.println(name + " 正在从 " + position + " 移动到 " + targetPosition);
        // 模拟移动过程
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        this.position = targetPosition;
        System.out.println(name + " 已到达目标位置: " + position);
        status = MachineStatus.IDLE;
    }
    
    /**
     * 设置路径规划策略
     */
    public void setPathPlanningStrategy(PathPlanningStrategy strategy) {
        this.pathStrategy = strategy;
        System.out.println(name + " 已设置路径规划策略: " + strategy.getClass().getSimpleName());
    }
    
    /**
     * 执行路径规划
     */
    public void planPath(Position start, Position end) {
        if (pathStrategy != null) {
            pathStrategy.planPath(this, start, end);
        } else {
            System.out.println("错误: " + name + " 未设置路径规划策略!");
        }
    }
    
    /**
     * 获取设备状态信息
     */
    public String getStatusInfo() {
        return String.format(
            "%s (%s)\n状态: %s\n位置: %s\n燃油: %.1f%%\n电量: %.1f%%",
            name, type, status, position, fuelLevel, batteryLevel
        );
    }
    
    /**
     * 执行特定作业任务，由子类实现
     */
    public abstract void performTask();
    
    /**
     * 处理环境数据变化，由子类实现观察者模式的响应方法
     */
    @Override
    public abstract void update(EnvironmentData environmentData);
} 